<?php
class Layout
{
    protected $_view_file = "";
    public static function getParams()
    {
        $params = explode("/",$_SERVER["REQUEST_URI"]);
        $new_params = array();
        foreach($params as $key => $param)
        {
            if(!empty($param))
            {
                //allow GET params
                $pos = strpos($param,"?");
                if($pos)
                    $param =  substr($param,0,$pos);
                    $new_params[]=$param;
                    
            }
        }
        return $new_params;
    }

public function render_dynamic_folder_subView(){
        $uri = self::getParams();
            if(!empty($uri)){
                    $controller_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/controller/");
                    $view_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/view/");
                    if(file_exists($controller_path) && file_exists($view_path))
                    {
                        if(isset($uri[1]))
                        {
                            $c = realpath($controller_path . "/index.php");
                            $v = realpath($view_path .  "/index.phtml");
                            if(file_exists($c) && file_exists($v))
                            {
                                $this->_view_file = $v;
                                include_once($c);
                            }
                            else
                            {
                                //Error Page
                                $this->_view_file = realpath(dirname(__FILE__) . "/../modules/error/view/index.phtml");
                                include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
                                
                            }
                        }else
                        {
                            //Automatic detection of index
                            $this->_view_file = realpath("{$view_path}/index.phtml");
                            include_once(realpath("{$controller_path}/index.php"));
                        }
                    }else
                    {
                        //Error Page
                        
                        $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/error/view/index.phtml");
                        include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
                        
                    }
                
                
            }else
            {
                //Home Page
                $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/index/view/index.phtml");
                include_once(realpath(dirname(__FILE__) . "/../modules/index/controller/index.php"));
            }
    }
    public function renderContent()
    {
        $uri = self::getParams();
        if(!empty($uri))
        {
            $controller_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/controller/");
            $view_path = realpath(dirname(__FILE__) . "/../modules/" . $uri[0] . "/view/");
            if(file_exists($controller_path) && file_exists($view_path))
            {
                if(isset($uri[1]))
                {
                    $c = realpath($controller_path . "/" . $uri[1] . ".php");
                    $v = realpath($view_path .  "/" . $uri[1] . ".phtml");
                    if(file_exists($c) && file_exists($v))
                    {
                        $this->_view_file = $v;
                        include_once($c);
                    }
                    else
                    {
                        //Error Page
                        $this->_view_file = realpath(dirname(__FILE__) . "/../modules/error/view/index.phtml");
                        include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
                    }
                }
                else
                {
                    //Automatic detection of index
                    $this->_view_file = realpath("{$view_path}/index.phtml");
                    include_once(realpath("{$controller_path}/index.php"));
                }
            }
            else
            {
                //Error Page
                $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/error/view/index.phtml");
                include_once(realpath(dirname(__FILE__) . "/../modules/error/controller/index.php"));
            }
        }
        else
        {
            //Home Page
            $this->_view_file = realpath(dirname(__FILE__) . "/../" . "/modules/index/view/index.phtml");
            include_once(realpath(dirname(__FILE__) . "/../modules/index/controller/index.php"));
        }
    }
    
    private function content()
    {
        include_once($this->_view_file);
    }
    
    private function header($header="header")
    {
        $layout = realpath(dirname(__FILE__) . "/../layout/{$header}.phtml");
        if(file_exists($layout))
        {
            include_once($layout);
        }
    }
    private function footer($footer="footer")
    {
        $layout = realpath(dirname(__FILE__) . "/../layout/{$footer}.phtml");
        if(file_exists($layout))
        {
            include_once($layout);
        }
    }
    private function setLayout($layout="")
    {
        if(empty($layout))
            include_once(realpath(dirname(__FILE__) . "/../" . "/layout/default.phtml"));
        else
            include_once(realpath(dirname(__FILE__) . "/../" . "/layout/{$layout}.phtml"));
    }


   
}

